package com.aiolos.wxdc.service.impl;

import com.aiolos.wxdc.dataobject.ProductInfo;
import com.aiolos.wxdc.dto.CartDTO;
import com.aiolos.wxdc.enums.ProductStatusEnum;
import com.aiolos.wxdc.enums.ResultEnum;
import com.aiolos.wxdc.exception.SellException;
import com.aiolos.wxdc.repository.ProductInfoRepository;
import com.aiolos.wxdc.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.List;
import java.util.NoSuchElementException;

@Service
public class ProductServiceImpl implements ProductService {

    @Autowired
    private ProductInfoRepository repository;

    @Override
    @Cacheable(cacheNames = "product",key = "123")
    public ProductInfo findById(String productInfoId) {
        return repository.findById(productInfoId).get();
    }

    @Override
    public List<ProductInfo> findUpAll() {
        return repository.findByProductStatus(ProductStatusEnum.UP.getCode());
    }

    @Override
    public Page<ProductInfo> findAll(Pageable pageable) {
        return repository.findAll(pageable);
    }

    @Override
    @CachePut(cacheNames = "product",key = "123")
    public ProductInfo save(ProductInfo productInfo) {
        return repository.save(productInfo);
    }

    @Override
    @Transactional
    public void increaseStock(List<CartDTO> cartDTOList) {
        for(CartDTO cartDTO:cartDTOList) {
            ProductInfo productInfo = repository.findById(cartDTO.getProductId()).get();
            if (productInfo == null) { //商品不存在
                throw new SellException(ResultEnum.PRODUCT_NOT_EXIST);
            }

            //增加库存
            Integer result = productInfo.getProductStock() + cartDTO.getProductQuantity();
            productInfo.setProductStock(result);
            repository.save(productInfo);
        }
    }

    @Override
    @Transactional
    public void decreaseStock(List<CartDTO> cartDTOList) {
        for(CartDTO cartDTO:cartDTOList){
            ProductInfo productInfo = repository.findById(cartDTO.getProductId()).get();
            if(productInfo == null){ //商品不存在
                throw new SellException(ResultEnum.PRODUCT_NOT_EXIST);
            }

            //减库存
            Integer resultStock = productInfo.getProductStock() - cartDTO.getProductQuantity();
            if(resultStock < 0){
                //库存不足
                throw new SellException(ResultEnum.PRODUCT_STOCK_ERROR);
            }

            //保存库存
            productInfo.setProductStock(resultStock);
            repository.save(productInfo);
        }
    }

    @Override
    public ProductInfo offSale(String productId) {
        ProductInfo productInfo = null;
        try {
            productInfo = repository.findById(productId).get();
        }catch (NoSuchElementException e){
            throw new SellException(ResultEnum.PRODUCT_NOT_EXIST);
        }
        //判断商品状态
        if(productInfo.getProductStatusEnum() == ProductStatusEnum.DOWN){
            throw new SellException(ResultEnum.PRODUCT_STATUS_ERROR);
        }
        //更新状态
        productInfo.setProductStatus(ProductStatusEnum.DOWN.getCode());
        return repository.save(productInfo);
    }

    @Override
    public ProductInfo onSale(String productId) {
        ProductInfo productInfo = null;
        try {
            productInfo = repository.findById(productId).get();
        }catch (NoSuchElementException e){
            throw new SellException(ResultEnum.PRODUCT_NOT_EXIST);
        }
        //判断商品状态
        if(productInfo.getProductStatusEnum() == ProductStatusEnum.UP){
            throw new SellException(ResultEnum.PRODUCT_STATUS_ERROR);
        }
        //更新状态
        productInfo.setProductStatus(ProductStatusEnum.UP.getCode());
        return repository.save(productInfo);
    }
}
